"use strict";
/* Controllers */
app.controller("myPasswordController", ['$scope', '$location', '$rootScope', '$routeParams', 'myPasswordService', function ($scope, $location, $rootScope, $routeParams, myPasswordService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.p.id = $routeParams.id; // 如果id

    var errorFn = function (result) {
        alert(result);
    };
    $scope.resetPassword = function () {
        var vm = $scope.vm;
        if(vm.p.password!=vm.p.password2) {
            alert("两次输入新密码不同！");
            return;
        }
        var users = {
            id: vm.p.id,
            userName: vm.p.userName,
            password: vm.p.password,
            password0: vm.p.password0
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                var code = response.data;
                if(code==-1) {
                    alert("执行异常！");
                }else if(code==-2) {
                    alert("旧用户名密码错误！");
                }else {
                    alert("执行成功！");
                }
            } else {
                alert(response.message);
            }
        };
        myPasswordService.resetPassword(users, procFn, errorFn);
    };



    $scope.initializeController = function () {
        if(vm.p.id!=0) {
            var users = {
                id: vm.p.id
            };
            var procFn = function (response) {
                if (response.success && response.data) {
                    vm.p = response.data;
                } else {
                    alert(response.message);
                }
            };
            myPasswordService.getUser(users, procFn, errorFn);
        }else {
            alert("账户不存在！");
        }
    };
    $scope.initializeController();

}]);


/* Services */
app.service('myPasswordService', ['ajaxService', function (ajaxService) {

    this.resetPassword = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "/star/api/basic-service/v1/user/resetPassword.do", successFunction, errorFunction);
    };

    this.getUser = function (users, successFunction, errorFunction) {
        ajaxService.AjaxPost(users, "/star/api/basic-service/v1/user/getUser.do", successFunction, errorFunction);
    };

}]);

